package controller.link.game;

import view.client.Console;
import model.Hearer;
import java.io.Serializable;
import controller.Link;
import controller.link.RefreshGameData;
import model.User;
import model.Attendant;
import model.Server;
import model.game.Location;

public class ChangeLocation implements Link, Serializable {

    private String location;

    public ChangeLocation(String location) {
        this.location = location;
    }

    public ChangeLocation(Hearer h, String location) {
        this.location = location;
        h.send((Link) this);
    }

    @Override
    public void serverUse(Server server, Attendant at) {
        User user = at.getUser();
        Location o = server.getWorld().getMap().findLocation(location);
        if (o != null) {
            server.getWorld().getMap().removePlayer(user.getPlayer());
            o.addPlayer(user.getPlayer());
        }
        RefreshGameData rgd = new RefreshGameData();
        rgd.serverUse(server, at);
        Ask4PlayersRoom apr = new Ask4PlayersRoom();
        for (int i = 0; i < server.getAttendants().size(); i++) {
            apr.serverUse(server, server.getAttendants().get(i));
        }
        at.send((Link) this);
    }

    @Override
    public void clientUse(Console console) {
        if (console.getDs()!=null){
            console.getDs().dispose();    
        }        
    }
}
